package com.lanou.dllo.findfood.adpter;

import android.content.Context;


import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.util.LruCache;
import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.entity.User_top_all;
import com.lanou.dllo.findfood.volley.VolleySington;


import java.util.List;


/**
 * Created by dllo on 15/10/22.
 */
public class HotUserlastAdpter extends RecyclerView.Adapter<HotUserlastAdpter.lastAdpter> {
    private List<User_top_all> alls;
    private int s;
    private RequestQueue queue;
    private LruCache<String, Bitmap> lruCache;
    private ImageLoader imageLoader;
    private ImageLoader.ImageCache imageCache;
    private Context context;


    public void addData(List<User_top_all> all) {
        alls = all;
        notifyDataSetChanged();
    }

    public HotUserlastAdpter(Context context) {
        // VolleySington.getInstance();
        this.context = context;
        queue = Volley.newRequestQueue(context);


    }

    class lastAdpter extends RecyclerView.ViewHolder {
        private NetworkImageView userimage;
        private TextView usname, sex, alllevel_title, reputation, picture_count,positi;
        private  ImageView gold;


        public lastAdpter(View itemView) {
            super(itemView);
            userimage = (NetworkImageView) itemView.findViewById(R.id.userimage);
            usname = (TextView) itemView.findViewById(R.id.usname);
            sex = (TextView) itemView.findViewById(R.id.sex);
            reputation = (TextView) itemView.findViewById(R.id.reputation);
            picture_count = (TextView) itemView.findViewById(R.id.picture_count);
            alllevel_title = (TextView) itemView.findViewById(R.id.alllevel_title);
            positi = (TextView) itemView.findViewById(R.id.position);
            gold = (ImageView) itemView.findViewById(R.id.gold);

        }
    }

    private void getBitmap(String url, final NetworkImageView networkImageView) {
        ImageLoader imageLoader = VolleySington.getInstance().getImageLoader();
        networkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
        networkImageView.setErrorImageResId(R.mipmap.ic_launcher);
        networkImageView.setImageUrl(url, imageLoader);


    }


    @Override
    public lastAdpter onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.rececy_user_last_item, null);


        return new lastAdpter(view);
    }

    @Override
    public void onBindViewHolder(lastAdpter holder, int position) {
        if (alls != null && alls.size() > 0) {
            User_top_all top_all = alls.get(position);

           holder.usname.setText(top_all.getName());
           holder.alllevel_title.setText(top_all.getLevel_title());
            int se = top_all.getSex();
          if (se == 0) {
                holder.sex.setText("女");
            } else if (se == 1) {
                holder.sex.setText("男");
            } else {
                holder.sex.setText("null");
            }
            String imUr = top_all.getUser_image();
            if (imUr != null && imUr.length() > 0) {


                getBitmap(imUr, holder.userimage);
            }
            holder.reputation.setText(top_all.getReputation() + "");
            holder.picture_count.setText(top_all.getPicture_count() + "");
            holder.positi.setText(position+1+"");
            if (position !=0){
                Bitmap bitmap = BitmapFactory.decodeResource(Resources.getSystem(),R.mipmap.gold_android);
                holder.gold.setImageBitmap(bitmap);
            }

        }


    }

    @Override
    public int getItemCount() {
        return alls != null && alls.size() > 0 ? alls.size() : 0;
    }


}
